﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：ComboBox自定义控件帮助类
//
//创建标识：齐李平 || 2011-04-28
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace Hmc.IM.Common.UserControls
{
    internal class NativeMethods
    {
        #region Const

        public static readonly IntPtr FALSE = IntPtr.Zero;
        public static readonly IntPtr TRUE = new IntPtr(1);

        /// <summary>
        /// 重绘
        /// </summary>
        public const int WM_PAINT = 0x000F;
        /// <summary>
        /// 当一个编辑型控件将要被绘制时发送此消息给它的父窗口；
        /// </summary>
        public const int WM_CTLCOLOREDIT = 0x133;

        #endregion

        #region ComboBoxButtonState
        /// <summary>
        /// 
        /// </summary>
        public enum ComboBoxButtonState
        {
            STATE_SYSTEM_NONE = 0,
            STATE_SYSTEM_INVISIBLE = 0x00008000,
            STATE_SYSTEM_PRESSED = 0x00000008
        }

        #endregion

        #region RECT
        /// <summary>
        /// 
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="left"></param>
            /// <param name="top"></param>
            /// <param name="right"></param>
            /// <param name="bottom"></param>
            public RECT(int left, int top, int right, int bottom)
            {
                Left = left;
                Top = top;
                Right = right;
                Bottom = bottom;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="rect"></param>
            public RECT(Rectangle rect)
            {
                Left = rect.Left;
                Top = rect.Top;
                Right = rect.Right;
                Bottom = rect.Bottom;
            }
            /// <summary>
            /// 
            /// </summary>
            public Rectangle Rect
            {
                get
                {
                    return new Rectangle(
                        Left,
                        Top,
                        Right - Left,
                        Bottom - Top);
                }
            }
            /// <summary>
            /// 
            /// </summary>
            public Size Size
            {
                get
                {
                    return new Size(Right - Left, Bottom - Top);
                }
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <param name="width"></param>
            /// <param name="height"></param>
            /// <returns></returns>
            public static RECT FromXYWH(int x, int y, int width, int height)
            {
                return new RECT(x,
                                y,
                                x + width,
                                y + height);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="rect"></param>
            /// <returns></returns>
            public static RECT FromRectangle(Rectangle rect)
            {
                return new RECT(rect.Left,
                                 rect.Top,
                                 rect.Right,
                                 rect.Bottom);
            }
        }

        #endregion

        #region PAINTSTRUCT
        /// <summary>
        /// 
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct PAINTSTRUCT
        {
            public IntPtr hdc;
            public int fErase;
            public RECT rcPaint;
            public int fRestore;
            public int fIncUpdate;
            public int Reserved1;
            public int Reserved2;
            public int Reserved3;
            public int Reserved4;
            public int Reserved5;
            public int Reserved6;
            public int Reserved7;
            public int Reserved8;
        }

        #endregion

        #region ComboBoxInfo Struct
        /// <summary>
        /// 
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct ComboBoxInfo
        {
            public int cbSize;
            public RECT rcItem;
            public RECT rcButton;
            public ComboBoxButtonState stateButton;
            public IntPtr hwndCombo;
            public IntPtr hwndEdit;
            public IntPtr hwndList;
        }

        #endregion

        #region API Methods

        /// <summary>
        /// 检索有关指定组合框
        /// </summary>
        /// <param name="hwndCombo">窗口句柄。</param>
        /// <param name="info">一个指针 COMBOBOXINFO 结构</param>
        /// <returns>如果函数成功，返回值为非零。如果函数失败，返回值为零</returns>
        [DllImport("user32.dll")]
        public static extern bool GetComboBoxInfo(
            IntPtr hwndCombo, ref ComboBoxInfo info);

        /// <summary>
        /// 该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
        /// </summary>
        /// <param name="hWnd">窗口句柄。</param>
        /// <param name="lpRect">指向一个RECT结构的指针，该结构接收窗口的左上角和右下角的屏幕坐标。</param>
        /// <returns>如果函数成功，返回值为非零：如果函数失败，返回值为零</returns>
        [DllImport("user32.dll")]
        public static extern int GetWindowRect(IntPtr hWnd, ref RECT lpRect);

        /// <summary>
        /// 函数为指定窗口进行绘图工作的准备，并用将和绘图有关的信息填充到一个
        /// </summary>
        /// <param name="hWnd">窗口的句柄</param>
        /// <param name="ps">指向一个用来接收绘画信息的PAINTSTRUCT结构</param>
        /// <returns>如果函数成功，返回值是指定窗口的“显示设备描述表”句柄。
        /// 如果函数失败，返回值是NULL，表明没有得到显示设备的内容</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);

        /// <summary>
        /// 函数标记指定窗口的绘画过程结束
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="ps">指向一个PAINTSTRUCT结构，该结构包含了绘画信息，是BeginPaint函数返回的返回</param>
        /// <returns>　返回值始终是非0</returns>
        [DllImport("user32.dll")]
        public static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);

        /// <summary>
        /// 得到指定的窗口的设备环境
        /// </summary>
        /// <param name="hWnd">窗口的句柄。</param>
        /// <returns>返回hWnd参数所指定的窗口的设备环境。</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);

        /// <summary>
        /// 函数释放设备上下文环境（DC）供其他应用程序使用
        /// </summary>
        /// <param name="hWnd">窗口的句柄。</param>
        /// <param name="hDC">指向要释放的设备上下文环境的句柄</param>
        /// <returns>返回值说明了设备上下文环境是否释放；如果释放成功，则返回值为1；如果没有释放成功，则返回值为0</returns>
        [DllImport("user32.dll")]
        public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);

        /// <summary>
        /// 该函数获取窗口客户区的坐标
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="r">客户区坐标</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetClientRect(IntPtr hWnd, ref RECT r);

        /// <summary>
        /// 这个函数判断指定的点是否位于矩形lpRect内部
        /// </summary>
        /// <param name="lprc">矩形内部</param>
        /// <param name="pt">欲判断的点</param>
        /// <returns>非零表示点在矩形内部，零表示点在矩形外部</returns>
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool PtInRect([In] ref RECT lprc, Point pt);

        #endregion
    }
}
